package com.beaut.project.util;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class ServletUtility {
	 /**
	   * 验证Session的有效性
	   * @param session        用户上下文会话
	   * @param key            检查参数的键名
	   * @return               true=在上下文会话中存在以key为名称的对象
	   *                       false=在上下文会话中不存在以key为名称的对象
	   */
	  public static boolean isValidSession(HttpSession session, String key) {
	    if (session == null || key == null) {
	      return false;
	    }
	    if (session.getAttribute(key) == null) {
	      return false;
	    }
	    return true;
	  }
	  /**
	   * 传递请求

	   * @param toUrl                        传递的URL地址
	   * @param request                      请求
	   * @param response                     回复
	   * @throws ServletException
	   * @throws IOException
	   */
	  public static void forward(String toUrl, HttpServletRequest request,
	      HttpServletResponse response) throws ServletException,
	      IOException {
	    RequestDispatcher requestDispatcher =
	        request.getRequestDispatcher(toUrl);
	    requestDispatcher.forward(request, response);
	  }
	  
	  public static boolean isLoginAction(HttpServletRequest request) {
		    if (request == null) {
		      return false;
		    }
		    String qUri = request.getRequestURI();
		    String contextPath = request.getContextPath();
		    if (contextPath.equals("")) {
		      contextPath = "/yh";
		    }
		    
		    if (qUri.endsWith("/")) {
		      qUri += "index.jsp";
		    }
		    if (!qUri.startsWith(contextPath)) {
		      qUri = contextPath + qUri;
		    }
		    if (qUri.equals(contextPath + "/login.jsp")
		        || qUri.equals(contextPath + "/index.jsp")
		        || qUri.equals(contextPath + "/public/sessionerror.jsp")) {
		      
		      return true;
		    }
		    return false;
		  }
}
